

import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

/**
 * @author Baysaupova Malika
 */
public class Millioneir
        extends MIDlet
        implements CommandListener,ItemStateListener{

    private Millioneire[] uqs;

    private Form mForm ;
    private Form helpForm;
    private Form hallForm;

    private Command exit,next,start,finnish, help, cmdHlpExit;
    private Command half, call, hall, takeMoney;
    //private Image im;

    private Image hallIm;

    private ChoiceGroup group;
    private int step;
    private int numberOfQuestions = 15;
    private Alert alert;
    private boolean t = false;
    private int[] money;
    private int res = 0;

    public Millioneir() throws IOException {
        uqs = new Millioneire[numberOfQuestions];
        money = new int[numberOfQuestions];
        initializeQuestions();
        String a = "Who wants to be a millioneir?";
        mForm = new Form("Millioneir");
        exit = new Command("Exit",Command.EXIT,1);
        next = new Command("Next",Command.SCREEN,2);
        start = new Command("Start",Command.SCREEN,3);
        finnish = new Command("Finnish",Command.SCREEN,4);
        help = new Command("How to play", Command.SCREEN, 5);
        cmdHlpExit = new Command("Back", Command.EXIT,6);

        half = new Command("50/50", Command.SCREEN, 7);
        call = new Command("Call to friend", Command.SCREEN, 8);
        hall = new Command("Auditopies help", Command.SCREEN, 9);
        takeMoney = new Command("I take money", Command.SCREEN, 10);

        String strHelp[] = {"In order to earn 1 million rubles, must correctly answer 15 questions from different fields of knowledge.",
                            "Each question has 4 possible answers, of which only one is correct. Each question has a specific value.",
                            "The player offers three tips:",
                            "50:50 - a computer eliminates two incorrect answers",
                            "Call a Friend - within 30 seconds, the player can consult with one of the five friends, declared in advance",
                            "Help Room - Everyone in the studio audience voted for the right, in his view, the answer, and the player statistics vote "};
         helpForm = new Form("How to play");
         for (int i = 0; i < 6; i++)
         helpForm.append(strHelp[i]);

         //im = Image.createImage("/images.png");
         //mForm.append(new ImageItem(null,im,ImageItem.LAYOUT_CENTER,null));

         hallIm = Image.createImage("/hall.png");
         hallForm.append(new ImageItem(null,hallIm,ImageItem.LAYOUT_CENTER,null));

         mForm.append(a);
         mForm.addCommand(exit);
         mForm.addCommand(start);
         mForm.addCommand(help);
         mForm.setCommandListener(this);

        helpForm.addCommand(cmdHlpExit);
        helpForm.setCommandListener(this);

        hallForm.setCommandListener(this);
        
    }
    public void initializeQuestions() {
        String question[] = {
            "Computers that operate in essentially the same way are said to be?",
            "A computer processes data in a device called the?",
            "Of the following types of memory, which is least expensive?",
            "Of the following types of storage, which provides the fastest access to data?",
            "Which of the layers of software that make up a computer system provides applications with direct access to the operating system?",
            "A trap is triggered by?",
            "Which of the following structures is used by operating systems to describe to a user how files and folders, excluding shortcuts, are organized on a storage device?",
            "Who of the following plans an entire information system?",
            "Embedding different types of data, such as sound and graphics, within Internet electronic mail requires which of the following formats to be used for encoding the data?",
            "An IP address is a set of four numbers between 0 and",
            "When you send an email to someone, it is sent to",
            "What is the protocol used to transfer Web pages?",
            "Which of the following computer attack methods does not require a hardware or software tool?",
            "A certificate authority associates a specific _____ with the entity requesting the certificate.",
            "To increase the likelihood of a successful restore operation from a previous backup operation, the backup file should be stored on which of the following?"
        };
        String answer1[] = {
            "compound",
            "compatible",
            "user-friendly",
            "integrated"
        };
        String answer2[] = {
            "device driver",
            "CPU",
            "server",
            "URL"
        };
        String answer3[] = {
            "Register",
            "L1 Cache",
            "DRAM",
            "L2 Cache"
        };
        String answer4[] = {
            "cache",
            "RAM",
            "CD-ROM",
            "hard disk"
        };
         String answer5[] = {
            "Device drivers",
            "Scripts",
            "The BIOS",
            "The API"
        };
          String answer6[] = {
            "an attempt to access a non-existent memory location",
            "the execution of processor instructions",
            "a power surge",
            "an external signal"
        };
           String answer7[] = {
            "a tree",
            "a matrix",
            "a root",
            "a list"
        };
            String answer8[] = {
            "Systems analyst",
            "Systems administrator",
            "Systems administrator",
            "Programmer"
        };
             String answer9[] = {
            "MIME",
            "GIF",
            "Encryption",
            "TCP/IP"
        };
              String answer10[] = {
            "24",
            "128",
            "255",
            "16"
        };
               String answer11[] = {
            "the sender's email client",
            "the sender's email server",
            "the recipient's email server",
            "the recipient's computer"
        };
                String answer12[] = {
            "http",
            "html",
            "ftp",
            "udp"
        };
                 String answer13[] = {
            "Spoofing",
            "Port scanning",
            "Social engineering",
            "Packet sniffing"
        };
                  String answer14[] = {
            "digital signature",
            "password",
            "public key",
            "public key"
        };
                   String answer15[] = {
            "A disk drive located on a file server",
            "The computer's least used disk drive",
            "The actual disk drive being backed up",
            "Another disk drive installed inside that computer"
        };
        uqs[0] = new Millioneire(question[0], answer1, 0, 1);
        uqs[1] = new Millioneire(question[1], answer2, 0, 1);
        uqs[2] = new Millioneire(question[2], answer3, 0, 2);
        uqs[3] = new Millioneire(question[3], answer4, 0, 0);
        uqs[4] = new Millioneire(question[4], answer5, 0, 3);
        uqs[5] = new Millioneire(question[5], answer6, 0, 1);
        uqs[6] = new Millioneire(question[6], answer7, 0, 0);
        uqs[7] = new Millioneire(question[7], answer8, 0, 0);
        uqs[8] = new Millioneire(question[8], answer9, 0, 0);
        uqs[9] = new Millioneire(question[9], answer10, 0, 2);
        uqs[10] = new Millioneire(question[10], answer11, 0, 2);
        uqs[11] = new Millioneire(question[11], answer12, 0, 0);
        uqs[12] = new Millioneire(question[12], answer13, 0, 2);
        uqs[13] = new Millioneire(question[13], answer14, 0, 2);
        uqs[14] = new Millioneire(question[14], answer15, 0, 0);
        money[0] = 100;
        money[1] = 200;
        money[2] = 300;
        money[3] = 500;
        money[4] = 1000;
        money[5] = 2000;
        money[6] = 4000;
        money[7] = 8000;
        money[8] = 16000;
        money[9] = 32000;
        money[10] = 64000;
        money[11] = 125000;
        money[12] = 250000;
        money[13] = 500000;
        money[14] = 1000000;
    }
    public void startApp() {
        Display.getDisplay(this).setCurrent(mForm);

    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if (c == help) Display.getDisplay(this).setCurrent(helpForm);

        if (c == cmdHlpExit) Display.getDisplay(this).setCurrent(mForm);
        
        if (c == exit) {
            destroyApp(false);
            notifyDestroyed();
        } else if (c == start && t == false) {
            step = 0;
            mForm.deleteAll();
            mForm.removeCommand(start);
            mForm.removeCommand(help);
            mForm.addCommand(next);

            mForm.addCommand(half);
            mForm.addCommand(call);
            mForm.addCommand(hall);
            mForm.addCommand(takeMoney);

            group = new ChoiceGroup(
                        uqs[step].getQuestion(),
                        ChoiceGroup.EXCLUSIVE,
                        uqs[step].getAnswers(),
                        null
                    );
            mForm.append(group);
            mForm.setCommandListener(this);
            mForm.setItemStateListener(this);
            Display.getDisplay(this).setCurrent(mForm);
            t= true;
            res = money[step];

        } else if (c == next && t == false) {
            step++;
            mForm.deleteAll();
            if (step == numberOfQuestions-1) {
                mForm.removeCommand(next);
                mForm.addCommand(finnish);
            }
            mForm.removeCommand(exit);
            group = new ChoiceGroup(
                        uqs[step].getQuestion(),
                        ChoiceGroup.EXCLUSIVE,
                        uqs[step].getAnswers(),
                        null
                    );
            group.setSelectedIndex(uqs[step].getChosenAnswer(), true);
            mForm.append(group);
            mForm.setCommandListener(this);
            mForm.setItemStateListener(this);
            Display.getDisplay(this).setCurrent(mForm);
                    t= true;
                    res = money[step];
        } else if ((c == next && t == true) ||
                    (c == start && t == true)) {

            if (uqs[step].getChosenAnswer() == uqs[step].getRightAnswer()) {
                    String text = "Your result is: " + res + " from 1 000 000";
                    alert = new Alert(
                    "CoNgRaTuLtIoNs",
                    text,
                    null,
                    AlertType.INFO);

                    alert.setTimeout(5000);
                    alert.addCommand(next);
                    alert.setCommandListener(this);
                    Display.getDisplay(this).setCurrent(alert);
                    t = false;
              }else {
                    String text = "Unfortunately, you answered wrong. Start a new game!";
                    alert = new Alert(
                    "eRRoR",
                    text,
                    null,
                    AlertType.INFO);

                    alert.setTimeout(5000);
                    alert.addCommand(exit);
                    alert.setCommandListener(this);
                    Display.getDisplay(this).setCurrent(alert);
                    t = false;
              }
                       
           
        }
    }

    public void itemStateChanged(Item item) {
        if (item == group) {
            uqs[step].setChosenAnswer(group.getSelectedIndex());
        }
    }
}